package com.ce.webframework.sec.util;


import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;

/**
*
* <p>版权所有：中企动力
* <p>未经本公司许可，不得以任何方式复制或使用本程序任何部分
* <p>功能描述：定时任务处理器
*
* @author kuanglingxuan
* Created on 2007-9-24
*/
public class TimerTask extends java.util.TimerTask {



	public TimerTask(Object object, String methodName, Object[] methodArgs){
		try {
			if(methodArgs != null)
				this.method = object.getClass().getMethod(methodName, methodArgs[0].getClass());
			else
				this.method = object.getClass().getMethod(methodName, null);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.timerId = this.toString();
		this.object = object;
		this.timer = new Timer();
		this.methodArgs = methodArgs;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		invodeMethod();
	}

	public void invodeMethod(){
		try {
			System.out.println("methodArgs---->"+methodArgs);
			method.invoke(object, methodArgs);
			if(this.isRepeat == false || this.isRepeat == null){
				this.stop();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


	public void start(Boolean isRepeat, Date firstTime, Long period){

//		this.method = object.methodName;
		System.out.println("starting..."+this);
		this.isRepeat = isRepeat;
		if(isRepeat == TimerTask.REPEAT && period != null){
			timer.schedule(this, firstTime, period);
		}else{
			timer.schedule(this, firstTime);
		}
	}

	public void initDaily(){
		System.out.println("init...");
		this.isRepeat = TimerTask.REPEAT;
		invodeMethod();//第一次调用
		SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
		Date now = new Date();
		String nowStr = date.format(now);
		try {
			now = time.parse(nowStr + " " + ZERO_HOUR);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}// 每天的00:00:00定时触发更新方法
		System.out.println("now------>" + now);

		timer.schedule(this, now, ONE_DAY);

	}

	public void stop(){
		timer.cancel();
	}

//	public void setArgument(Object object, String methodName, Date date){
//		try {
//			this.method = object.getClass().getMethod(methodName, null);
//
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		this.object = object;
//	}


	public static final String ZERO_HOUR = "23:59:59";
	public static final Integer ONE_DAY = 60*60*24*1000;
	public static final Boolean REPEAT = true;
	public static final Boolean NOT_REPEAT = false;


	private Timer timer;
	private Method method;
	private Object object;
	private Boolean running;

	private Object[] methodArgs;
	private Boolean isRepeat;
	private String timerId;

	public String getTimerId() {
		return timerId;
	}

	public void setTimerId(String timerId) {
		this.timerId = timerId;
	}

	public Boolean isRunning() {
		return running;
	}

	public void setRunning(Boolean running) {
		this.running = running;
	}

	public Timer getTimer() {
		return timer;
	}


}
